#include <graphics.h>
#include "Sprite.h"
#include "input.h"
#include "UI.h"
#include "SceneManager.h"
#include "AssetManager.h"
#include "Scene.h"
#include "AudioAsset.h"
#include "collision.h"
#include <chrono>
#include <windows.h>
using namespace GameEngine;
float vy = 0.0f;
const float gravity = 0.5f;
const float jumpSpeed = -12.0f;
const float groundY = 400.0f;
int main() {
    // ��ʼ��ͼ�δ���
    initgraph(1280, 720);

    //��Ƶ����
    AudioPlayer audio1,audio2;
    audio1.init("D:/code_easyx/Project1/Project1/audio/bgm.mp3", "test", false);
    audio2.init("D:/code_easyx/Project1/Project1/audio/barb_break.mp3", "test_loop", true);

    //UI����
    TextBox text(600, 360, 50, 50, "Test_UI");

    //ʹ��Ԥ���ص���Դ����
    //AssetManager* manager;
    //auto atlas_run = manager->CreateAsset<TextureAsset>("Run", L"D:/code_easyx/Project1/Project1/img/run.png");
    auto atlas_run = new TextureAtlasAsset("Run", _T("D:/code_easyx/Project1/Project1/img/player_right_%d.png"), 6);
    atlas_run->Load();
    Animation an_run(atlas_run, 100, true);
    Sprite player_test("Run", an_run);
    player_test.SetPos(200,500);

    //����λ����Ϣ
    TextureAsset* background_t = new TextureAsset("background", _T("D:/code_easyx/Project1/Project1/img/background.png"));
    //��λ����Ϣ
    Texture background(background_t, Vector2(0, 0));
    background.load();
    //loadimage(&background, _T("D:/code_easyx/Project1/Project1/img/background.png"));
    
    // �������봦����
    //��ʹ��Ԥ���ص���Դ����
    InputHandler input;
    Sprite patman;
    patman.AddState("right", _T("D:/code_easyx/Project1/Project1/img/player_right_%d.png"), 6, 100, true);
    // �����������
    Sprite player;

    // ���ض��������辫��ͼΪˮƽ���е�֡��
    IMAGE spriteSheet;
    loadimage(&spriteSheet, _T("D:/code_easyx/Project1/Project1/img/run.png")); // ��ȷ��·����ȷ
    
    IMAGE jumpSpriteSheet;
    loadimage(&jumpSpriteSheet, _T("D:/code_easyx/Project1/Project1/img/jump.png"));
    Animation attack("attack", _T("D:/code_easyx/Project1/Project1/img/vfx_attack_right.png"), 5, 200, false);
    //����״̬����֡���Ƿ�ѭ������
    player.AddState("Run", _T("D:/code_easyx/Project1/Project1/img/run.png"), 10, 100, true); // 6֡�����100ms��ѭ������
    player.AddState("Jump", _T("D:/code_easyx/Project1/Project1/img/jump.png"), 5, 100, false);
    player.AddState("Fall", _T("D:/code_easyx/Project1/Project1/img/fall.png"), 5, 100, false);
    //���û���������һ��״̬
    player.SetTransition("Jump", "Fall");
    player.SetTransition("Fall", "Run");
    //���ӷ�תͼ��
    player.AddFlipAnim("Run", "Run_flip");
    player.AddFlipAnim("Jump", "Jump_flip");
    player.AddFlipAnim("Fall", "Fall_flip");
    patman.AddFlipAnim("right", "left");
    // ���ó�ʼ״̬
    Vector2 pos;
    pos.x = 100;
    pos.y = 100;
    Vector2 v(1,1);
    Vector2 pos2(200, 500);
    patman.SetState("right", pos, v);
    player_test.SetState("Run", Vector2 (200, 500));
    player.SetState("Run",pos2,v);
    Camera cam(1280,720);
    BeginBatchDraw();
    // ��ѭ��
    bool facingLeft = false;   // �����־
    bool jumping = false;      // ��Ծ�б�־
    //��ײ����
    RectangleCollisionBox patman_col1(200,500,60,80);
    RectangleCollisionBox patman_col2(100,100,60,80);
    while (true) {
        cleardevice();
        //putimage(0, 0, &background);
        // ����֡ʱ�䣨����ÿ֡Լ16ms��
        int delta = 16;  
        //������Ƶ��ͣ����
        if (input.isKeyDown('P')) {
            if (audio1.isPlaying()) {
                audio1.pause();
            }
            else {
                audio1.play();
            }
        }
        if (input.isKeyDown('O')) {
            if (audio2.isPlaying()) {
                audio2.pause();
            }
            else {
                audio2.play();
            }
        }
        //��������ͷ����
        if (input.isKeyDown('R')) {
            cam.shake(10,100); //��Ļ����
        }
        if (input.isKeyDown('1')) {
            cam.SmoothCameraMovement(patman.position, 1); //����ͷ�ƶ���ʹ��ɫ�����м�λ��

        }
        if (input.isKeyDown('2')) {
            cam.Move(player.position); //����ͷ������ʹ��ɫ�����м�λ��
        }
        if (input.isKeyDown('Q')) {
            player.ChangeState(facingLeft ? "Run_flip" : "Run"); //�л�״̬
        }

        background.Draw(cam);
        // ��������״̬
        if (text.isClicked(input.getMouseX(), input.getMouseY())) {
            std::cout << "Test pass" << std::endl;
        }
        input.update();
        //��������
        if (!jumping && input.isKeyDown(VK_SPACE)) {
            jumping = true;
            vy = jumpSpeed;
            player.Play(facingLeft ? "Jump_flip" : "Jump",false);
        }
        //d) ���������ƶ������ڷ���Ծ�п������ܶ���
        // ÿ֡ģ������
        if (jumping) {
            vy += gravity;
            player.Move(0, vy);

            // ��ؼ��
            if (player.position.y >= groundY) {
                player.position.y =  groundY;
                vy = 0;
                jumping = false;
                player.ChangeState(facingLeft ? "Run_flip" : "Run");
            }
        }
        text.draw();
        float dx = 0, dy = 0;
        //����ģ�����Լ�����֡������
        //������л���������֡���ֵ�����������ʽ��һ����ѭ�涨��һ���Զ���
        if (input.isKeyDown('A')) {
            dx -= 1; 
            facingLeft = true;
            player.SwitchFacingDuringPlay(true);
            patman.SwitchSimultaneously("left");
        }
        if (input.isKeyDown('D')) {
            facingLeft = false;
            dx += 1; 
            //player.changeState(facingLeft ? "Run_flip" : "Run");
            player.SwitchFacingDuringPlay(false);
            patman.SwitchSimultaneously("right");
        }
        if (!jumping) {
            if (input.isKeyDown('W')) dy -= 1;
            if (input.isKeyDown('S')) dy += 1;
        }

        if (dx != 0 || dy != 0) {
            float length = sqrt(dx * dx + dy * dy);
            dx /= length;
            dy /= length;
            player.Move(dx * 6, dy * 6);
            patman.Move(dx * 3, dy * 3);
        }

        // ���¾��鶯��
        //cam.SmoothCameraMovement(patman.position, 1); //����ͷ�ƶ���ʹ��ɫ�����м�λ��
        bool is_attack = false;
        if (input.isKeyDown('J')) {
            is_attack = true;
        }
        else if(attack.check_finished()){
            is_attack = false;
            attack.reset();
        }

        //����״̬�ͻ���
        cam.on_update(delta);
        player_test.Update(delta);
        player_test.Draw(cam); 
        patman_col1.setX(player_test.position.x);
        patman_col2.setY(player_test.position.y);
        player.Update(delta);
        player.Draw(cam);
        patman_col2.setX(patman.position.x);
        patman_col2.setY(patman.position.y);
        patman.Update(delta);
        patman.Draw(cam);

        //������ײ
        if (patman_col1.checkCollisionWithRectangle(patman_col2)) {
            std::cout << "Collision test pass" << std::endl;
        }
        //���Ի���������֡����ʧ
        if (!attack.check_finished() && is_attack) {
            attack.on_update(delta);
            attack.on_draw(cam,player.position.x + 100,player.position.y - 100);
        }
        // �ӳ��Կ���֡��
        FlushBatchDraw();
        Sleep(delta);
    }

    // �ر�ͼ�δ���
    closegraph();
    return 0;
}
